#include "main.h"
#include "global.h"

float tableau[2][2][2];

void func_help(){
  fprintf(stderr,"Utilisation\n");
  fprintf(stderr,"nepovito[-r FILE | -n]\n");
  fprintf(stderr,"-r FILE:\tRead a binary file\n");
  fprintf(stderr,"-n NETWORK:\tNetwork to sniff\n");
  return;
}

void print_app_banner(void) {
	printf("%s - %s\n", APP_NAME, APP_DESC);
	printf("%s\n", APP_COPYRIGHT);
	printf("%s\n", APP_DISCLAIMER);
	printf("\n");
	return;
}

void* start_pcap(void* name){
  init_pcap();
}

/* programme principal */
int main( int argc, char* argv[] )
{
  char *dev, errbuf[PCAP_ERRBUF_SIZE];
  pcap_t *handle;
  pthread_t captureHandle;
  char format[] = "hdr:w:n:";
  int optch;
  extern int opterr;
  char * network;
  int setRead=0;
  char * myFile;

  opterr = 1;

  while((optch = getopt(argc, argv, format)) != -1)
    switch(optch) {
      case 'n':
        network = optarg;
        break;
      case 'r':
        setRead=1;
        myFile = optarg;
        break;
      case 'h':
        func_help();
        return 0;
        break;
      default:
        func_help();
    }

  if (!network){
    fprintf(stderr, "Network address must be specified\n");
    return -1;
  }

  print_app_banner();
  /* Initialisations */
  glutInit( &argc, argv );
  //glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); /* double buffer */
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  glutInitWindowSize( width, height);
  glutCreateWindow("Analyse reseau");
  glClearColor( 0.4, 0.4, 0.6, 0.0 );
  //glShadeModel (GL_SMOOTH);
  printf("Tapez h pour obtenir de l'aide\n");
  printf("Tapez ESCAPE pour quitter l'application\n");

  /* Fonctions pouvant etre appelees dans la boucle principale */
  glutReshapeFunc( reshape );   /* redimensionnement fenetre */
  glutDisplayFunc( display );   /* affichage */
  glutKeyboardFunc( key );      /* touches du clavier */
  glutMouseFunc( mouse );       /* boutons de la souris */
  glutMotionFunc( mouseMotion );/* deplacement de la souris */
  glutIdleFunc(idle);

  /* Initialisation des données*/
  //init_net3D();
  x=calloc(1,sizeof(int));
  y=calloc(1,sizeof(int));
  z=calloc(1,sizeof(int));
  x[0]=10;
  y[0]=0;
  z[0]=0;
  myData=calloc(1,sizeof(net3D));
  myData->dec_ipv4 = 10;
  myData->src_port = 0;
  myData->dst_port = 0;
  myData->nb=1;
  count++;
  if (setRead){
    read_data(myFile);
  }
  /* Initialisation de pcap */ 
  check_requirements();
  if (pthread_create(&captureHandle, NULL, start_pcap, "AA")) { 
   perror("pthread_create"); 
   exit(EXIT_FAILURE); 
  }

  //init_pcap();
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  glEnable(GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  /* Lancement de la boucle principale */
  glutMainLoop();

  /* Cette instruction n'est jamais executee */
  return 0;
}

